/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.service;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.momarketing.dao.MicroProductDao;
import com.boxin.ims.modules.momarketing.entity.MicroProduct;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 微网站产品Service
 * @author Jakemanse
 * @version 2013-11-25
 */
@Component
@Transactional(readOnly = true)
public class MicroProductService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(MicroProductService.class);
	
	@Autowired
	private MicroProductDao microProductDao;
	
	public MicroProduct get(Long id) {
		return microProductDao.findOne(id);
	}
	
	
	public List<MicroProduct> findMicroProductsByProjectId(Long projectId){
		DetachedCriteria dc = microProductDao.createDetachedCriteria();
		dc.createAlias("project", "project");
		dc.add(Restrictions.eq("project.id", projectId));
		dc.addOrder(Order.desc("sort"));
		return microProductDao.find( dc);
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-25  下午11:32:31
	 * @function <p> 按分类查询 </p>
	 * @param categoryId
	 * @return
	 */
	public List<MicroProduct> findMicroProductsByCategoryId(Long categoryId){
		DetachedCriteria dc = microProductDao.createDetachedCriteria();
		dc.createAlias("category", "category");
		dc.add(Restrictions.eq("category.id", categoryId));
		dc.addOrder(Order.desc("sort"));
		return microProductDao.find( dc);
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-26  上午12:05:00
	 * @function <p> 首页推荐产品 </p>
	 * @param projectId
	 * @return
	 */
	public List<MicroProduct> findIndexProductsByProjectId(Long projectId){
		DetachedCriteria dc = microProductDao.createDetachedCriteria();
		dc.add(Restrictions.eq("project.id", projectId));
		dc.add(Restrictions.eq("displayType", "1"));
		dc.addOrder(Order.desc("sort"));
		return microProductDao.find( dc);
	}
	
	
	
	public Page<MicroProduct> find(Page<MicroProduct> page, MicroProduct microProduct) {
		DetachedCriteria dc = microProductDao.createDetachedCriteria();
		if (microProduct.getUser()!=null && microProduct.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", microProduct.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(microProduct.getName())){
			dc.add(Restrictions.like("name", "%"+microProduct.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return microProductDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(MicroProduct microProduct) {
		microProduct.setDescription(StringEscapeUtils.unescapeHtml4(microProduct.getDescription()));
		microProductDao.save(microProduct);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		microProductDao.deleteById(id);
	}
	
}
